package com.lanou.moving.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.activity.PlayMusicActivity;
import com.lanou.moving.adapter.SongListDetailAdapter;
import com.lanou.moving.base.BaseFragment;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.MusicInfo;
import com.lanou.moving.entity.SongListDetail;
import com.lanou.moving.entity.dao.RecentlyPlay;
import com.lanou.moving.entity.dao.RecentlyPlayDao;
import com.lanou.moving.eventBus.MyEventBus;
import com.lanou.moving.singleton.DaoSingleton;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by 梁树珍 on 15/11/3.
 */
public class NewSongJapanese extends BaseFragment implements ListView.OnItemClickListener {
    private ListView japanese_listView;
    private StringRequestUtf requestUtf;
    private VolleySingleton volleySingleton;
    private String url;
    private List<SongListDetail.SongsEntity> songsEntities;
    private SongListDetailAdapter adapter;
    public static String picUrl;
    private RecentlyPlayDao recentlyPlayDao;
    private List<MusicInfo> musicInfoList;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.new_song_japanese, null);
        volleySingleton = VolleySingleton.getInstance(mContext);
        recentlyPlayDao = DaoSingleton.getInstance().getRecentlyPlayDao();
        japanese_listView = (ListView) view.findViewById(R.id.japanese_listView);

        japanese_listView.setOnItemClickListener(this);
        getDatas();
        return view;
    }

    private void getDatas() {
        url = URL.NEW_SONG_START + 97258648 + URL.NEW_SONG_END;
        requestUtf = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SongListDetail detail = gson.fromJson(response, SongListDetail.class);
                songsEntities = detail.getSongs();
                adapter = new SongListDetailAdapter(mContext, songsEntities);
                japanese_listView.setAdapter(adapter);
                picUrl = detail.getImage().getPic();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(requestUtf);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        SongListDetail.SongsEntity songsEntity =
                (SongListDetail.SongsEntity) parent.getItemAtPosition(position);
        if ((songsEntity.getUrlList() != null && songsEntity.getUrlList().size() > 0)) {
            RecentlyPlay recentlyPlay = new RecentlyPlay();
            recentlyPlay.setSongName(songsEntity.getName());
            recentlyPlay.setPlayUrl(songsEntity.getUrlList().get(0).getUrl());
            recentlyPlay.setDuration(songsEntity.getUrlList().get(0).getDuration());
            recentlyPlay.setSingerName(songsEntity.getSingerName());
            recentlyPlay.setSongId((long) songsEntity.getSongId());
            recentlyPlay.setResource("网络");
            recentlyPlayDao.insertOrReplace(recentlyPlay);

            EventBus.getDefault().post(new MyEventBus.PlayMusic(1));
            musicInfoList = new ArrayList<>();

            for (SongListDetail.SongsEntity entity : songsEntities) {
                if (entity.getUrlList() != null && entity.getUrlList().size() > 0) {
                    MusicInfo musicInfo = new MusicInfo();
                    musicInfo.setSongName(entity.getName());
                    musicInfo.setAlbum(entity.getAlbumName());
                    musicInfo.setAlbumId((int) entity.getAlbumId());
                    musicInfo.setUrl(entity.getUrlList().get(0).getUrl());
                    musicInfo.setDuration(entity.getUrlList().get(0).getDuration());
                    musicInfo.setSingerName(entity.getSingerName());
                    musicInfo.setResouce("网络");
                    musicInfoList.add(musicInfo);
                }
            }

            Intent intent = new Intent(mContext, PlayMusicActivity.class);
            EventBus.getDefault().post(new MyEventBus.Deliver(musicInfoList, songsEntity.getName()));
            intent.putExtra("songName", songsEntity.getName());
            intent.putExtra("duration", songsEntity.getUrlList().get(0).getDuration());
            intent.putExtra("resource", "网络");
            intent.putExtra("albumId", songsEntity.getAlbumId());
            intent.putExtra("singerName", songsEntity.getSingerName());
            startActivity(intent);
        } else {
            Toast.makeText(mContext, "暂时无法播放此歌曲", Toast.LENGTH_LONG).show();
        }
    }
}
